// 过滤器
import Vue from 'vue';

// 去掉空字符串
Vue.filter("trim", value => {
	if(!value){
		return "";
	}
	
	value = value.toString();

	return value.replace(/(^\s*)|(\s*$)/g, "");
});


// 格式化日期
Vue.filter("dateformat", value => {
	if(!value){
		return "";
	}

	let currentDate = new Date(),
	 	startDate = new Date(value.replace(/-/g, '/')),
	 	time = value.toString().substr(11, 5),
	 	chineseWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
		dis = startDate.getTime() - currentDate.getTime();
	if(dis <= 0){
		return chineseWeek[startDate.getDay()] + ' ' + time;
	}else if(dis >= 7*24*3600*1000 && dis < 7*2*24*3600*1000){
		return '下' + chineseWeek[startDate.getDay()] + ' ' + time;
	}else if(dis >= 3 * 7 * 24*3600*1000){
		return value;
	}else{
		let timestamp = (currentDate.getHours() * 3600 + currentDate.getMinutes() * 60 + currentDate.getSeconds()) * 1000;
		let weekendTimeStamp = currentDate.getTime() + (7 - currentDate.getDay() + 1) * 86400000 - timestamp;
		if(startDate.getTime() <= weekendTimeStamp){
			return chineseWeek[startDate.getDay()] + ' ' + time;
		}else{
			return '下' + chineseWeek[startDate.getDay()] + ' ' + time;
		}
	}
});


Vue.filter("countdown", value => {
	if(!value || !sessionStorage['hcxsq_servertime']){
		return "";
	}

	let startTime = +value,
		serverTime = +sessionStorage['hcxsq_servertime'];


	let countDownTime = startTime - serverTime;

	if(countDownTime <= 0){
        return '00:00:00';
    }

    let day, hour, mins, secs;
    
    hour = Math.floor(countDownTime / 3600000);
    mins = Math.floor((countDownTime - hour * 3600000) / 60000);
    secs = (countDownTime - hour * 3600000 - mins * 60000) / 1000;

    hour = hour < 10? "0"+ hour: hour;
    mins = mins < 10? "0"+ mins: mins;
    secs = secs < 10? "0"+ secs: secs;

    return countDownTime >= 3 * 7 * 24*3600*1000? '' :  hour + ':' + mins + ':' + secs;
});